package org.example.user;

import java.io.*;
import java.net.Socket;
import java.util.List;

public class ReceiveMsgRunnable implements  Runnable{

    boolean running = true;
    Socket socket;
    DataInputStream dis;

    public ReceiveMsgRunnable(Socket s) throws IOException {
        this.socket = s;
        InputStream is = s.getInputStream();
        dis = new DataInputStream(is);
    }

    private String receive(){
        String msg = "";
        try {
           msg =  dis.readUTF();
        } catch (IOException e) {
            running = false;
            throw new RuntimeException(e);
        }
        return msg;
    }

    private  void  release(){
        try {
            if(dis!=null)
                dis.close();
            if (socket!=null){
                socket.close();
            }
        } catch (IOException e) {
            running = false;
            throw new RuntimeException(e);
        }

    }
    /**
     * When an object implementing interface <code>Runnable</code> is used
     * to create a thread, starting the thread causes the object's
     * <code>run</code> method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method <code>run</code> is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    public void run() {
        while (running){
            String msg1 = receive();
            if( !msg1.equals(""));
            System.out.println(msg1);
        }
    }
}
